package com.woniuxy.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.woniuxy.entity.Client;
import com.woniuxy.service.ClientService;

import java.util.Date;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.utils.Result;

@RestController //所有数据默认以json返回
@Controller
@RequestMapping("/client")
public class ClientController {
	
	@Autowired
	private ClientService clientService;
	
	
	@GetMapping("findcname")
	public List<Client> findClientOfCname(){
	return clientService.findClientOfCname();
	}
	
	
	@ResponseBody
	@RequestMapping("/all")
	public List<Client> findCliends(){
		return clientService.findAllClients();
	}
	
	
	@ResponseBody
	@RequestMapping("/addClient")
	public Result<Object> addClient(Client client){
		Result<Object> result = new Result<>();
		client.setAddtime(new Date());
		int addResult = clientService.addClient(client);

		if (addResult != 0) {
			result.setStatus(result.SUCCESS);
			result.setMessage("客户新增成功");
		} else {
			result.setStatus(result.ERROR);
			result.setMessage("客户新增失败");
		}
		return result;
	}
	
	
	// 通过cid查询当前行的数据
	@ResponseBody
	@RequestMapping("/searchData")
	public Client searchData(int cid) {
		return clientService.findClientByCid(cid);
	}
	
	
	// 修改当前数据
	@ResponseBody
	@RequestMapping("/modifyClient")
	public Result<Object> searchData(Client client) {
		System.out.println(client.toString());

		Result<Object> result = new Result<>();
		client.setAddtime(new Date());
		int newResult = clientService.modifyClient(client);
		if (newResult != 0) {
			result.setStatus(result.SUCCESS);
			result.setMessage("数据修改成功");
		} else {
			result.setStatus(result.ERROR);
			result.setMessage("数据修改失败");
		}
		return result;
	}
	
	
	// 通过gfid删除当前数据
	@ResponseBody
	@RequestMapping("/deleteClient")
	public Result<Object> changeStatus(int cid) {
		Result<Object> result = new Result<>();
		int delResult = clientService.deleteDataByCid(cid);
		if (delResult != 0) {
			result.setStatus(result.SUCCESS);
			result.setMessage("数据删除成功");
		} else {
			result.setStatus(result.ERROR);
			result.setMessage("数据删除成功");
		}
		return result;
	}
}
